<?php

class MakeQueryController extends GxController {

    public function filters() {
        return array('rights');
    }

    public function actionAdmin() {
        $model = new MakeQuery('search');
        $model->unsetAttributes();
        if (isset($_GET['classid']) && $_GET['classid'] <> "") {
            $_GET['MakeQuery']['classes_id'] = $_GET['classid'];
            $model->setAttributes($_GET['MakeQuery']);
        } elseif (isset($_GET['courseid']) && $_GET['courseid'] <> "") {
            $_GET['MakeQuery']['courses_id'] = $_GET['courseid'];
            $model->setAttributes($_GET['MakeQuery']);
        } else {
            $_GET['MakeQuery']['classes_id'] = 0;
            $model->setAttributes($_GET['MakeQuery']);
        }
        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function actionclassCoursequery() {
        $model = new MakeQuery('search');
        $model->unsetAttributes();
        if (isset($_GET['MakeQuery']))
            $model->setAttributes($_GET['MakeQuery']);
        $this->render('view', array(
            'model' => $model,
        ));
    }

    public function actionremindtoTrainer() {
        $queryDeatls = MakeQuery::model()->findByPk($_POST['queryid']);
        $datetime1 = new DateTime(date('Y-m-d', strtotime($queryDeatls->created)));
        $datetime2 = new DateTime(date('Y-m-d'));
        $difference = $datetime1->diff($datetime2);
        $time = $difference->days . 'days';

        if ($_POST['classId'] != '0') {
            $mailto = $queryDeatls->createdBy->username;
            $classCourse = getClassName($_POST['classId']);
        }if ($_POST['courseId'] != '0') {
            $mailto = $queryDeatls->createdBy->username;
            $classCourse = getcourseName($_POST['courseId']);
        }
        $subject = "Your query by $queryDeatls->name has been pending since $time since query";
        $messageVal = "Your query by $queryDeatls->name for class/workshop  $classCourse has been pending since $time since query";
        $mailmodel = EmailTemplate::model()->findByPk(Message::TRINER_QUERY_REMINDER);
        $userText = $mailmodel->body;
        $mailbody = array('value' => $userText, 'type' => 'raw');
        $inboxLink = '<a href=' . SITE_URL . 'mailConversation/inbox/' . '>View Inbox</a>';
        $arrayFind = array('[username]', '[from_user_name]', '[classcoursename]', '[time]', '[activation_link]');
        $arrayReplace = array($mailto, $queryDeatls->name, $classCourse, $time, $inboxLink);
        $mailbody = str_replace($arrayFind, $arrayReplace, $mailbody);
        $mailtemp = Template::model()->findByPk($mailmodel->template_id);
        $body = $this->renderPartial('/mails/' . $mailtemp->filename, array('mailbody' => $mailbody['value']), true, false);
        $model = new ContactAdmin();
        $model->setAttribute('subject', $subject);
        $model->setAttribute('query', $messageVal);
        $model->setAttribute('backend_user_id', Message::ADMIN_ID);
        $model->setAttribute('from_users_id', $queryDeatls->created_by);
        $model->setAttribute('created', SkillConfig::CreateDateTime());
        $model->setAttribute('is_read', SkillConfig::STATUS_DEACTIVE);
        $model->save(FALSE);
        try {
            SkillSendMail::SendMail($mailto, $subject, $body);
            $result = 'success';
        } catch (Exception $e) {
            $result = $e;
            exit;
        }

        if ($result == 'success') {
            echo 'success';
        }
        Yii::app()->end();
    }

    public function actionqueryDeatils($id) {
        $this->layout = FALSE;
        if (Yii::app()->request->isAjaxRequest) {
            $this->renderPartial('view', array(
                'model' => $this->loadModel($id),
                'asDialog' => !empty($_GET['asDialog']),
                    ), false, true);
            Yii::app()->end();
        }
        else
            $this->render('_view', array(
                'model' => $this->loadModel($id, 'MakeQuery'),
            ));
    }

}